package ie.dcu.spca.view.product;

import java.util.List;
import java.util.Map;

import ie.dcu.spca.Main;
import ie.dcu.spca.R;
import ie.dcu.spca.data.BasicDataManager;
import ie.dcu.spca.data.ItemDataManager;
import ie.dcu.spca.data.ProductDataManager;
import ie.dcu.spca.model.bean.Product;
import ie.dcu.spca.view.DataList;
import ie.dcu.spca.view.item.ItemCRUDDialog;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class ProductSearchActivity extends BarcodeScanResultActivity {

    private static ProductDataManager productManager;

    private AutoCompleteTextView etKeyword;

    private TextView tvName;
    private TextView tvBarcode;
    private TextView tvPrice;
    private Button okButton;
    private Button cancelButton;
    private Button barcodeScanButton;

    private int productId;
    private int billId;
    private boolean isAdd;
    private int itemId;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.product_search_view);
        init();
    }

    private void init() {
        productManager = Main.dataManager.productDataManager();
        Bundle bundle = this.getIntent().getExtras();

        isAdd = bundle.getBoolean(DataList.IS_ADD);
        if (isAdd) {
            itemId = ItemDataManager.NEW_ITEM_ID;
            productId = productManager.getDefaultProductId();
        } else {
            itemId = bundle.getInt(BasicDataManager.ID);
            productId = bundle.getInt(BasicDataManager.PRODUCT_ID);
        }

        billId = bundle.getInt(BasicDataManager.BILL_ID);

        bindWidgets();

        etKeyword.setAdapter(getIdNameBarcodePriceAdapter(this, productManager
                .getIdNameBarcodePriceMapList()));

        etKeyword.setText(scanResultContents);
    }

    private void bindWidgets() {
        etKeyword = (AutoCompleteTextView) findViewById(R.id.autoCompleteKeywordTextField);
        cancelButton = (Button) findViewById(R.id.cancelOptButton);
        okButton = (Button) findViewById(R.id.okOptButton);
        barcodeScanButton = (Button) findViewById(R.id.barcodeScanButton);

        tvName = (TextView) findViewById(R.id.nameTextView);
        tvBarcode = (TextView) findViewById(R.id.barcodeTextView);
        tvPrice = (TextView) findViewById(R.id.priceTextView);

        setResultValues(productId);
        scanResultContents = tvBarcode.getText().toString();

        etKeyword.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapter, View view,
                    int position, long id) {
                Map<String, Object> map = (Map<String, Object>) adapter
                        .getItemAtPosition(position);
                productId = (Integer) map.get(BasicDataManager.ID);
                setResultValues(productId);
            }
        });

        scanResultTextEdit = etKeyword;

        barcodeScanButton
                .setOnClickListener(new BarcodeScanButtonOnClickListener(this));

        okButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent nextPageIntent = new Intent();
                Bundle bundle = new Bundle();

                bundle.putInt(BasicDataManager.ID, itemId);
                bundle.putInt(BasicDataManager.BILL_ID, billId);
                bundle.putInt(BasicDataManager.PRODUCT_ID, productId);
                scanResultContents = etKeyword.getText().toString();
                bundle.putString(BasicDataManager.BARCODE, scanResultContents);
                bundle.putBoolean(DataList.IS_ADD, isAdd);

                nextPageIntent.putExtras(bundle);

                nextPageIntent.setClass(ProductSearchActivity.this,
                        ItemCRUDDialog.class);
                startActivity(nextPageIntent);
                ProductSearchActivity.this.finish();
            }
        });

        cancelButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ProductSearchActivity.this.finish();
            }
        });
    }

    private void setResultValues(int productId) {
        Product product = productManager.get(productId);

        tvName.setText(product.getName());
        tvBarcode.setText(product.getBarcode());
        tvPrice.setText(productManager.getPriceAndUnit(product));
    }

    private IdNameBarcodePriceAdapter getIdNameBarcodePriceAdapter(
            Activity parent, List<Map<String, Object>> data) {
        IdNameBarcodePriceAdapter adapter = new IdNameBarcodePriceAdapter(
                parent, data, R.layout.product_info_view, new String[] {
                        BasicDataManager.NAME, BasicDataManager.BARCODE,
                        BasicDataManager.PRICE }, new int[] {
                        R.id.nameTextView, R.id.barcodeTextView,
                        R.id.priceTextView });
        return adapter;
    }
}

class BarcodeScanButtonOnClickListener implements OnClickListener {

    private Activity parent;

    public BarcodeScanButtonOnClickListener(Activity parent) {
        this.parent = parent;
    }

    public void onClick(View v) {
        Intent intent = new Intent("com.google.zxing.client.android.SCAN");
        intent.putExtra("SCAN_MODE", "ONE_D_MODE");
        parent.startActivityForResult(intent, 0);
    }
}
